﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace Estimate.Common.Repository
{
    /// <summary>
    /// 月度积分表。保存每个地区台站在一天的成绩。根据最新的自动站实况数据计算出台站的成绩
    /// </summary>
    public class MonthScoreRepository :BaseRepository
    {

        public MonthScoreRepository(string connectionString):base(connectionString,"MonthScore")
        {
        }

        public override string GetCreateTableSql()
        {
            string sqlcreate = String.Format("CREATE TABLE [{0}] (id bigint identity(1,1),caldate date, startdate date,enddate date, scorestring text) \n", _tableName);
            sqlcreate += String.Format("CREATE CLUSTERED INDEX [{0}_INDEX] ON [{1}] (startdate,enddate,caldate) \n", _tableName, _tableName);
            sqlcreate += String.Format("alter table [{0}] add primary key (id)", _tableName);
            return sqlcreate;
        }
        
        /// <summary>
        /// 查看月度报表日期是否存在
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        public bool IsRecordExist(DateTime startDate, DateTime endDate)
        {
            string sql = String.Format("SELECT * FROM [{0}] WHERE startdate=@startdate  and enddate=@enddate", _tableName);
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            return DBHelper.Exists(_connectionString, sql, param);
        }

        /// <summary>
        /// 获取月度报表的字符串值
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        public string GetScoreString(DateTime startDate, DateTime endDate)
        {
            string sql = String.Format("SELECT scorestring FROM [{0}] WHERE startdate=@startdate  and enddate=@enddate", _tableName);
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
             SqlConnection connection = new SqlConnection(_connectionString);
            SqlDataReader reader = DBHelper.ExecuteReader(connection, sql,param);
            string scorestring = "";
            if (reader.Read())
                scorestring = reader["scorestring"].ToString();
            connection.Close();
            return scorestring;
        }

        public void InsertScoreString(DateTime startDate, DateTime endDate,DateTime calDate, string scoreString)
        {
            string sql = String.Format("INSERT INTO [{0}] (startdate,enddate,caldate,scorestring) values(@startdate ,@enddate ,@caldate ,@scorestring)", _tableName);
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@caldate", calDate.ToString("yyyyMMdd"));
            param[3] = new SqlParameter("@scorestring", scoreString);
            DBHelper.ExecuteSql(_connectionString, sql, param);
        }

        public void UpdateScoreString(DateTime startDate, DateTime endDate,DateTime calDate, string scoreString)
        {
            string sql = String.Format("UPDATE [{0}] SET caldate=@caldate, scorestring=@scorestring WHERE startdate=@startdate  and enddate=@enddate", _tableName);
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@caldate", calDate.ToString("yyyyMMdd"));
            param[3] = new SqlParameter("@scorestring", scoreString);
            DBHelper.ExecuteSql(_connectionString, sql, param);
        }

        public void SetScoreString(DateTime startDate, DateTime endDate, string scorestring)
        {
            if (IsRecordExist(startDate, endDate))
                UpdateScoreString(startDate, endDate,DateTime.Now ,scorestring);
            else
                InsertScoreString(startDate, endDate, DateTime.Now, scorestring);
          
        }
    }
}
